export function getUrlFromData(data: Record<string, any>) {
  const arr: string[] = [];
  Object.keys(data).forEach((key) => {
    if (data[key]) arr.push(`${key}=${data[key]}`);
  });
  return arr.join('&');
}
interface MessagePayload {
  [key: string]: any;
}

interface MessageMetadata {
  [key: string]: any;
}

/**
 * 解析消息格式（泛型版本）
 * @param action 消息动作类型
 * @param payload 消息负载数据
 * @param metadata 元数据
 * @return 结构化消息对象
 */
export function parseMsg<
  T extends MessagePayload = object,
  M extends MessageMetadata = object,
>(
  action: string,
  payload: T = {} as T,
  metadata: M = {} as M,
): {
  meta: { timestamp: number } & M;
  data: {
    action: string;
    payload: T;
  };
} {
  const meta = {
    timestamp: Date.now(),
    ...metadata,
  };

  return {
    meta,
    data: {
      action,
      payload,
    },
  };
}
